package com.aote.entity.entityfinder;

import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;

import com.aote.util.StringHelper;
import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.util.RequestHelper;

/**
 * 根据页面中的多个属性查找实体
 * 
 */
public class MultiattributeCompoundingFinder implements EntityFinder {

	public Entity getEntity(HttpServletRequest request, Element element,
			HTMLBasicComponent component) {
		// 得到实体类型,查找的属性条件，需要查找的类名
		String entityType = element.getAttribute("entitytype");
		String attrCond = element.getAttribute("attrcond");
		String str = RequestHelper.replaceParameter(attrCond, request
				.getParameterMap(), false);
		// 如果没有#，说明全部条件都有属性值，可进行实体查找，否则，不进行查找
		if (str.indexOf('#') != -1) {
			return null;
		}
		// 根据条件，实体类型查找实体
		Map map = StringHelper.parseAttributes(str);
		Collection coll = EntityManager.getInstance().getEntities(entityType,
				map, component.getPage());
		if (coll.isEmpty()) {
			return null;
		} else {
			Entity entity = (Entity) coll.iterator().next();
			return entity;
		}
	}
}
